const Koa = require('koa2')
const app = new Koa()

const indent = (n) => new Array(n).join('&nbsp;')

const mid1 =  async (ctx, next) => {
  ctx.body = '<h2>请求=> 第一层响应中间件</h2>'
  await next() 
  ctx.body += '<h2>响应<= 响应第一层中间件</h2>'
}

const mid2 =  async (ctx, next) => {
  ctx.body += `<h2>${indent(4)}请求=> 第二层响应中间件</h2>`
  await next() 
  ctx.body += `<h2>${indent(4)}响应<= 响应第二层中间件</h2>`
}

const mid3 = async (ctx, next) => {
    ctx.body += `<h2>${indent(8)}请求=> 第三层响应中间件</h2>`
    await next() 
    ctx.body += `<h2>${indent(8)}响应<= 响应第三层中间件</h2>`
}
app.use(mid1)
app.use(mid2)
app.use(mid3)

app.use(async (ctx, next) => {
  ctx.type = 'text/html; cartset=utf-8'
  ctx.body += `<h1 style="color: red">${indent(12)}Koa核心业务</h1>`
})

app.listen(8000, () => console.log(8000))